package com.platform.common.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * TODO List工具类
 * 
 * @author zhanghan
 * @date 2016-10-19
 */
public class ListUtil {

	private static Logger log = Logger.getLogger(ListUtil.class);

	/**
	 * TODO list分页
	 * 
	 * @author zhanghan
	 * @date 2016-12-7
	 * @param list
	 * @param page
	 *            第几页
	 * @param rows
	 *            每页显示多少
	 * @return 返回分页后的List
	 */
	@SuppressWarnings("rawtypes")
	public static List listPage(List list, Integer page, Integer rows) {
		if (list == null || list.size() <= 0) {
			return list;
		}
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 20;
		}
		Integer start = (page - 1) * rows;
		Integer end = page * rows;

		if (start > list.size()) {
			return new ArrayList();
		}
		if (end > list.size()) {
			end = list.size();
		}
		List ll = list;
		try {
			ll = list.subList(start, end);
		} catch (Exception e) {
			log.error("List分页发送异常:" + e);
		}
		return ll;
	}

	public static void asc(List<?> list, String sort) {
		desc(list, sort);
		Collections.reverse(list);
	}

	public static void sort(List<?> list, final String sortField, final String sortMode) {
		if (StringUtils.isNotEmpty(sortMode) && sortMode.equalsIgnoreCase("desc")) {
			desc(list, sortField);
		} else {
			asc(list, sortField);
		}
	}

	/**
	 * @param targetList
	 *            目标排序List
	 * @param sortField
	 *            排序字段(实体类属性名,或者Map的字段名)
	 * @param sortMode
	 *            排序方式(asc or desc)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void desc(List<?> targetList, final String sortField) {
		try {
			if (targetList != null && targetList.size() > 0) {
				Collections.sort(targetList, new Comparator() {
					public int compare(Object arg1, Object arg2) {
						if (arg1 == null && arg2 == null) {
							// 如果两个同时为null,则判断两个相等
							return 0;
						} else if (arg1 == null) {
							// 如果第一个为null,则判断第一个小
							return -1;
						} else if (arg2 == null) {
							// 如果第二个为null,则判断第二个小
							return 1;
						} else if (arg2 != arg1 && null != arg2) {
							try {
								Object o1 = null;
								Object o2 = null;
								if (arg1 instanceof Map) {
									Map<String, Object> obj1 = (Map<String, Object>) arg1;
									Map<String, Object> obj2 = (Map<String, Object>) arg2;
									o1 = obj1.get(sortField);
									o2 = obj2.get(sortField);
								} else {
									// 首字母转大写
									String newStr = sortField.substring(0, 1).toUpperCase() + sortField.replaceFirst("\\w", "");
									String methodStr = "get" + newStr;
									// 创建两个实体的get方法
									Method method1 = arg1.getClass().getMethod(methodStr, null);
									Method method2 = arg2.getClass().getMethod(methodStr, null);
									o1 = method1.invoke(arg1, null);
									o2 = method2.invoke(arg2, null);
								}
								return compareByType(o1, o2);
							} catch (Exception e) {
								throw new RuntimeException();
							}
						} else {
							return -1;
						}
					}

					private int compareByType(Object obj1, Object obj2) {
						int retVal = 0;
						if (null == obj1 && null == obj2) {
							// 如果两个同时为null,则判断两个相等
							return retVal;
						}
						if (null == obj1) {
							// 因为是倒序,所以如果第一个为null,则第一个大
							return 1;
						}
						if (null == obj2) {
							// 因为是倒序,如果第二个为null,则第二个大
							return -1;
						}
						// 默认倒序
						if (obj2 instanceof Integer) {
							retVal = ((Integer) obj2).compareTo((Integer) obj1);
						} else if (obj2 instanceof Date) {
							// 日期
							retVal = ((Date) obj2).compareTo((Date) obj1);
						} else if (obj2 instanceof Double) {
							retVal = ((Double) obj2).compareTo((Double) obj1);
						} else if (obj2 instanceof Long) {
							retVal = ((Long) obj2).compareTo((Long) obj1);
						} else if (obj2 instanceof Float) {
							retVal = ((Float) obj2).compareTo((Float) obj1);
						} else {
							// 长度相等时比较ASCII值
							retVal = obj2.toString().compareTo(obj1.toString());
						}
						return retVal;
					}
				});
			}
		} catch (Exception e) {
			log.error("List排序发生异常:" + e);
		}
	}

	/**
	 * 
	 * @Description:去除重复元素,需要重写hashcode(),equals()方法
	 * @date:Jan 13, 2018 2:35:57 PM
	 * @param list
	 * @return void 返回类型
	 * @throws
	 */
	public List<?> removeDuplicate(List<?> list) {
		if (list != null && list.size() > 0) {
			HashSet h = new HashSet(list);
			list.clear();
			list.addAll(h);
		}
		return list;
	}

	/**
	 * 
	 * @Description:去除重复元素数量,需要重写hashcode(),equals()方法
	 * @date:Jan 13, 2018 2:35:57 PM
	 * @param list
	 * @return void 返回类型
	 * @throws
	 */
	public int removeDuplicateTotal(List<?> list) {
		int count = 0;
		if (list != null && list.size() > 0) {
			int total = list.size();
			HashSet h = new HashSet(list);
			list.clear();
			list.addAll(h);
			count = total - list.size();
		}
		return count;
	}

}
